﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;

namespace HeathCareSystem
{
    public class NguoiDungDAO
    {
        DataProvider dp = new DataProvider();

        public List<NguoiDungDTO> LayDanhSachBacSi()
        {
            List<NguoiDungDTO> listKetQua = new List<NguoiDungDTO>();
            NguoiDungDTO ndDTO = new NguoiDungDTO();
            DataTable dt = new DataTable();
            string strSQL = string.Format("SELECT NguoiDung.MaNguoiDung, NguoiDung.HoTen " +
                                          "FROM TaiKhoan INNER JOIN NguoiDung ON TaiKhoan.MaTaiKhoan = NguoiDung.MaTaiKhoan");
            dt = dp.ExecuteQuery(strSQL);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                ndDTO = new NguoiDungDTO();
                ndDTO.maNguoiDung = dt.Rows[i][0].ToString();
                ndDTO.hoTen = dt.Rows[i][1].ToString();
                //ndDTO.gioiTinh = dt.Rows[i][2].ToString();
                //ndDTO.ngaySinh = DateTime.Parse(dt.Rows[i][3].ToString());
                //ndDTO.diaChi = dt.Rows[i][4].ToString();
                //ndDTO.soDienThoai = dt.Rows[i][5].ToString();
                //ndDTO.queQuan = dt.Rows[i][6].ToString();
                //ndDTO.hocVi = dt.Rows[i][7].ToString();
                //ndDTO.soNamKinhNghiem = Int32.Parse(dt.Rows[i][8].ToString());
                //ndDTO.maTaiKhoan = Int32.Parse(dt.Rows[i][9].ToString());
                listKetQua.Add(ndDTO);
            }
            return listKetQua;
        }

        public bool ThemNguoiDung(NguoiDungDTO nd)
        {
            string strSQL = string.Format("INSERT INTO NguoiDung (MaNguoiDung, HoTen, GioiTinh, NgaySinh, DiaChi, SoDienThoai, QueQuan, HocVi, SoNamKinhNghiem, MaTaiKhoan, DaXoaNguoiDung) "+
                                          "VALUES ('{0}', N'{1}', N'{2}', CONVERT(DATETIME, '{3}', 102), N'{4}', '{5}', N'{6}', N'{7}', {8}, {9}, '{10}')", 
                                          nd.maNguoiDung, nd.hoTen, nd.gioiTinh, nd.ngaySinh, nd.diaChi, nd.soDienThoai, nd.queQuan, nd.hocVi, nd.soNamKinhNghiem, nd.maTaiKhoan, nd.daXoaNguoiDung);
            return dp.ExecuteNonQuery(strSQL);
        }

        public bool XoaNguoiDung(string MaNguoiDung)
        {
            string strSQL = string.Format("DELETE FROM NguoiDung WHERE (MaNguoiDung = '{0}')", MaNguoiDung);
            return dp.ExecuteNonQuery(strSQL);
        }

        public bool CapNhatNguoiDung(NguoiDungDTO nd)
        {
            string strSQL = string.Format("UPDATE NguoiDung "+
                                          " SET HoTen = N'{0}', GioiTinh = N'{1}', NgaySinh = CONVERT(DATETIME, '{2}', 102), DiaChi = N'{3}', SoDienThoai = '{4}', "+
                                          " QueQuan = N'{5}', HocVi = N'{6}', SoNamKinhNghiem = {7}, MaTaiKhoan = {8}, DaXoaNguoiDung = '{9}' " +
                                          " WHERE (MaNguoiDung = '{10}')",
                                          nd.hoTen, nd.gioiTinh, nd.ngaySinh, nd.diaChi, nd.soDienThoai, nd.queQuan, nd.hocVi, nd.soNamKinhNghiem, nd.maTaiKhoan, nd.daXoaNguoiDung,  nd.maNguoiDung);
            return dp.ExecuteNonQuery(strSQL);
        }

        //tao ma benh nhan tu dong
        public String TaoMaNguoiDungTuDong()
        {
            // Chuỗi cố định trong ma nguoi dung
            string strChu;
            //tao ngay
            DateTime dtNow = DateTime.Now;

            string temp = dtNow.Year.ToString();
            string strYear = temp.Substring(2, 2);
            strChu = strYear;
            String strSo = "0";

            String strMaDau = strChu + "0000";

            String strSql = "SELECT TOP 1 MaNguoiDung FROM NGUOIDUNG ORDER BY MaNguoiDung DESC";

            try
            {
                String strMaCuoi = dp.ExecuteScalar(strSql).ToString();
                String strTemp = ((int.Parse(strMaCuoi.Remove(0, 4)) + 1)).ToString();
                String strMaBenhNhan = strChu;
                while (strMaBenhNhan.Length + strTemp.Length < 6)
                    strMaBenhNhan += strSo;
                return strMaBenhNhan + strTemp;

            }
            catch (Exception)
            {
                return strMaDau;
            }
        }
    }
}